using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Brand.Dto;
using JTZY.Model.Brand;
using JTZY.Repository;
using JTZY.Service.Brand.IBrandService;

namespace JTZY.Service.Brand
{
    /// <summary>
    /// 品牌表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsbrandService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsbrandService : BaseService<Corecmsbrand>, ICorecmsbrandService
    {
        /// <summary>
        /// 查询品牌表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsbrandDto> GetList(CorecmsbrandQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Corecmsbrand, CorecmsbrandDto>(parm);

            return response;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<Corecmsbrand> GetAllList(CorecmsbrandQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToList();

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsbrand GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加品牌表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsbrand AddCorecmsbrand(Corecmsbrand model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改品牌表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsbrand(Corecmsbrand model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsbrand> QueryExp(CorecmsbrandQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsbrand>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Name), it => it.Name.Contains(parm.Name));
            return predicate;
        }
    }
}